#include "TestLogPluginImp.h"


extern "C" HTTP_DLL_API void* K5_Bundle_Activator(void)
{
    IBundleActivator* pBund = 0;
    pBund = TestLogPluginImp::Instance();
    return (void*)pBund;
}

TestLogPluginImp* TestLogPluginImp::m_pInstance = 0;
/********************************************************************************
 * @name function TestLogPluginImp::Instance
 * @function description：
 * @param
 * @param
 * @param
 * @return
********************************************************************************/
TestLogPluginImp * TestLogPluginImp::Instance()
{
    if(m_pInstance == NULL)
        m_pInstance = new TestLogPluginImp();
    return m_pInstance;
}

/********************************************************************************
 * @name function TestLogPluginImp::TestLogPluginImp
 * @function description：
 * @param
 * @param
 * @param
 * @return
********************************************************************************/
TestLogPluginImp::TestLogPluginImp()
{
    m_pBundCnt = NULL;
    m_pServMgr = NULL;
    m_pAlloc = NULL;
    m_pLogger = NULL;
}

/********************************************************************************
 * @name function TestLogPluginImp::~TestLogPluginImp
 * @function description：
 * @param
 * @param
 * @param
 * @return
********************************************************************************/
TestLogPluginImp::~TestLogPluginImp()
{
    if(m_pBundCnt != NULL)
        delete m_pBundCnt;
    m_pBundCnt = NULL;

    if(m_pServMgr != NULL)
        delete m_pServMgr;
    m_pServMgr = NULL;

    if(m_pAlloc != NULL)
        delete m_pAlloc;
    m_pAlloc = NULL;

    if(m_pLogger != NULL)
        delete m_pLogger;
    m_pLogger = NULL;
}


/********************************************************************************
 * @name function TestLogPluginImp::setServiceMnager
 * @function description：
 * @param
 * @param
 * @param
 * @return
********************************************************************************/
bool TestLogPluginImp::setServiceMnager(IBundleContext* pBundleContext)
{

    IServiceManager * pServiceManager = 0;
    int iRet = pBundleContext->GetServiceManager(&pServiceManager);
    if((SUCCESS != iRet) || (0 == pServiceManager))
    {
        m_pServMgr = 0;
        return false;
    }
    m_pServMgr = pServiceManager;
    return true;
}

/********************************************************************************
 * @name function TestLogPluginImp::setBundleContext
 * @function description：
 * @param
 * @param
 * @param
 * @return
********************************************************************************/
bool TestLogPluginImp::setBundleContext(IBundleContext* pBundleContext)
{

    if(0 == pBundleContext)
        return false;
    m_pBundCnt = pBundleContext;
    return true;
}

/********************************************************************************
 * @name function TestLogPluginImp::setAllocator
 * @function description：
 * @param
 * @param
 * @param
 * @return
********************************************************************************/
bool TestLogPluginImp::setAllocator(IBundleContext* pBundleContext)
{

    IAllocator * pAllocator = 0;
    pBundleContext->GetAllocator(&pAllocator);
    if(0 == pAllocator) return false;
    m_pAlloc = pAllocator;
    return true;
}

/********************************************************************************
 * @name function TestLogPluginImp::setLogger
 * @function description：
 * @param
 * @param
 * @param
 * @return
********************************************************************************/
bool TestLogPluginImp::setLogger(IBundleContext* pBundleContext)
{
    IService * pLogFac = 0;
    m_pServMgr->GetService(IID_LoggerFactory,&pLogFac,SID_LoggerFactory,0);
    if(0 == pLogFac)
        return false;
    ILoggerFactory *pLoggerFactory = (ILoggerFactory *)pLogFac;

    m_pLogger = pLoggerFactory->CreateBundleLogger("TestLogPlugin.log", "TestLogPlugin");
    if(m_pLogger == NULL)
    {
        return false;
    }

    return true;

}


/********************************************************************************
 * @name function TestLogPluginImp::Logger
 * @function description：
 * @param
 * @param
 * @param
 * @return
********************************************************************************/
ILogger * TestLogPluginImp::Logger()
{
    return m_pLogger;
}


/********************************************************************************
 * @name function TestLogPluginImp::Start
 * @function description：
 * @param
 * @param
 * @param
 * @return
********************************************************************************/
bool OSGI_API TestLogPluginImp::Start(IBundleContext* pBundleContext)
{
    printf("TestLogPluginImp::Start\n");
    bool bRet = false;
    try
    {
        //初始化
        if(setBundleContext(pBundleContext) != true)
            return bRet;
        if(setAllocator(pBundleContext) != true)
            return bRet;
        if(setServiceMnager(pBundleContext) != true)
            return bRet;
	//if(setLogger(pBundleContext) != true)
      //      return bRet;

	printf("111111!\n");
	IService * pLogFac = 0;
    m_pServMgr->GetService(IID_LoggerFactory,&pLogFac,SID_LoggerFactory,0);
    if(0 == pLogFac)
        return false;
    ILoggerFactory *pLoggerFactory = (ILoggerFactory *)pLogFac;

    m_pLogger = pLoggerFactory->CreateBundleLogger("TestLogPlugin.log", "TestLogPlugin");
    if(m_pLogger == NULL)
    {
        return false;
    }

	m_pLogger->DebugLog("FT121","setLogger3333333");

	m_pLogger->TraceLog("FT121","test1111111");
	m_pLogger->InfoLog("FT121","test2222222");
	m_pLogger->DebugLog("FT121","test3333333");
	m_pLogger->WarnLog("FT121","test14444444");
	m_pLogger->ErrorLog("FT121","test5555555");
	m_pLogger->FatalLog("FT121","test6666666");
	        
	printf("2222222!\n");
	
	IService* pTestLog = TestLogPlugin::GetInstance();

        int iRet = m_pServMgr->RegisterService(IID_TestLogger,
                                                pTestLog,
                                                SID_TestLogger, 0);
        //int iRet = m_pServMgr->RegisterService(IID_Logger,
        //                                        pTestLog,
        //                                        SID_AppLogger, 0);
        if(iRet == 0)
        {
            bRet = true;
        }
        else
        {
            printf("TestLogService start failed\n");
        }

    }
    catch(...)
    {
        bRet = false;
        return bRet;
    }

	//m_pLogger->Destroy();
    return bRet;
}


/********************************************************************************
 * @name function TestLogPluginImp::Stop
 * @function description：
 * @param
 * @param
 * @param
 * @return
********************************************************************************/
bool OSGI_API TestLogPluginImp::Stop(IBundleContext* pBundleContext)
{
    bool bRet = false;
    try
    {
        //do something
        bRet = true;
    }
    catch(...)
    {
        bRet = false;
        return bRet;
    }
    return bRet;
}

